$(function () {
    // 获取验证码
    $("#rgCodebtn").click(function () {
        par = {};
        par.phone = $(".register-body input[name=phone]").val();
        var data = JSON.stringify(par);
        $.ajax({
            url: "/p2p/SMSCode",
            contentType: "application/json",
            async: false,
            datatype: "json",
            data: data,
            type: 'POST',
            success: function (result) {
                if(result.statusCode=="5001"){
                    cookieNull(result.statusCode);
                }
                if (result.statusCode == '2000') {
                    var data = result.data;
                    console.log(data.smsVerificationCode);
                    pop("您的验证码为  " + data.smsVerificationCode,function(){
                        new invokeSettime("#rgCodebtn");
                        $(".popBox").remove();
                    });


                } else {
                    pop(result.message,function () {
                        $(".popBox").remove();
                    })

                }
            }
        });
    })
    // 注册
    $("#registerBTN").click(function () {
        par = {};
        par.phone = $(".register-body input[name=phone]").val();
        par.smsVerificationCode = $(".register-body input[name=smsVerificationCode]").val();
        par.passWd = $(".register-body input[name=passWd]").val();
        par.passWord = $(".register-body input[name=passWord]").val();
        var data = JSON.stringify(par);
        console.log(data)
        $.ajax({
            url: "/p2p/register",
            contentType: "application/json",
            async: false,
            datatype: "json",
            data: data,
            type: 'POST',
            success: function (result) {
                if(result.statusCode=="5001"){
                    cookieNull(result.statusCode);
                }if(result.statusCode=="2000"){
                    pop(result.message,function(){
                        $(".lg-body").show().siblings().hide();
                        $("input").val("");
                        $(".popBox").remove();
                    })
                }else {
                    pop(result.message,function () {
                        $(".popBox").remove();
                    })

                }

            }
        });
    });
    // 登录
    $("#longinBtn").click(function () {
        par = {};
        var phone = $(".lg-body input[name=phone]").val();
        par.phone = phone
        par.passWd = $(".lg-body input[name=passWd]").val();
        var data = JSON.stringify(par);
        $.ajax({
            url: "/p2p/longin",
            contentType: "application/json",
            async: false,
            datatype: "json",
            data: data,
            type: 'POST',
            success: function (result) {
                if(result.statusCode=="5001"){
                    cookieNull(result.statusCode);
                }
                if (result.statusCode == "2000") {
                    $.cookie('token', result.data.token, {path: '/p2p/static'});
                    $.cookie('phone', phone, {path: '/p2p/static'});

                    // pop(result.message)
                    $("#rgFont").css('display', 'none')
                    $("#lgFont").css('display', 'none')
                    $("#notAdmin").css('display', 'block')
                    $(".firstBody").css('display', 'block')
                    $(".body").css('display', 'none')
                    window.location.href = '/p2p/static/index.html';
                } else {
                    pop(result.message,function () {
                        $(".popBox").remove();
                    })

                }
            }
        });
    });

    $("#quitFont").click(function () {
        pop("是否退出登录", function () {
            $.cookie('token', null);
            $.cookie('phone', null);
            $("#rgFont").css('display', 'block')
            $("#lgFont").css('display', 'block')
            $("#notAdmin").css('display', 'none')
            $(".firstBody").css('display', 'block')
            $(".body").css("display", "none")
            window.location.href = '/p2p/static/index.html';
            $(".popBox").remove();
        })

    })
    //admin退出
    $("#adminQuitFont").click(function () {
        pop("是否退出登录", function () {
            $.cookie('token', null);
            $.cookie('phone', null);
            $("#rgFont").css('display', 'block')
            $("#lgFont").css('display', 'block')
            $("#notAdmin").css('display', 'none')
            $(".firstBody").css('display', 'block')
            $(".body").css("display", "none")
            window.location.href = '/p2p/static/index.html';
            $(".popBox").remove();
        })

    })

});
// 个人中心
$(function () {
    par = {};
    var token = $.cookie('token');
    par.token = token;
    var phone = $.cookie('phone');
    par.phone = phone;
    // 实名认证
    $("#realcertificationBTN").click(function () {
        par.userName = $("#realcertification input[name=userName]").val()
        par.idCard = $("#realcertification input[name=idCard]").val()
        var data = JSON.stringify(par)
        $.ajax({
                url: "/p2p/realcertification",
                contentType: "application/json",
                async: false,
                datatype: "json",
                data: data,
                type: 'POST',
                success: function (result) {
                    if(result.statusCode=="5001"){
                        cookieNull(result.statusCode);
                    }
                    if(result.statusCode=="2000"){
                        pop(result.message,function(){
                            $("#notAdmin").click();
                            $("input").val("");
                            $(".popBox").remove();
                        })
                    }else {
                        pop(result.message,function () {
                            $(".popBox").remove();
                        })

                    }

                }
            }
        )
    })

    // 设置交易密码
    $("#newpaypdBTN").click(function () {
        var newPaymentPassword = $("#newpaypd input[name=newPaymentPassword]").val()
        var nPaymentPassword = $("#newpaypd input[name=new-PaymentPassword]").val()
        if (newPaymentPassword != nPaymentPassword) {
            pop("两次密码不正确，请再次输入")
            return
        } else {
            par.newPaymentPassword = newPaymentPassword;
            var data = JSON.stringify(par);
            $.ajax({
                    url: "/p2p/newpaypd",
                    contentType: "application/json",
                    async: false,
                    datatype: "json",
                    data: data,
                    type: 'POST',
                    success: function (result) {
                        if(result.statusCode=="5001"){
                            cookieNull(result.statusCode);
                        }
                        if(result.statusCode=="2000"){
                            pop(result.message,function(){
                                $("#notAdmin").click();
                                $("input").val("");
                                $(".popBox").remove();
                            })
                        }else {
                            pop(result.message,function () {
                                $(".popBox").remove();
                            })

                        }
                    }
                }
            )
        }
    })

    // 绑定银行卡
    $("#bindingbankcardBTN").click(function () {
        par.idCard = $("#bindingbankcard input[name=idCard]").val()
        par.bankName = $("#bindingbankcard input[name=bankName]").val()
        par.cardholderName = $("#bindingbankcard input[name=cardholderName]").val()
        par.bankCardNumber = $("#bindingbankcard input[name=bankCardNumber]").val()
        par.paymentPassword = $("#bindingbankcard input[name=paymentPassword]").val()
        var data = JSON.stringify(par);
        $.ajax({
                url: "/p2p/bindingbankcard",
                contentType: "application/json",
                async: false,
                datatype: "json",
                data: data,
                type: 'POST',
                success: function (result) {
                    if(result.statusCode=="5001"){
                        cookieNull(result.statusCode);
                    }if(result.statusCode=="2000"){
                        pop(result.message,function(){
                            $("#notAdmin").click();
                            $("input").val("");
                            $(".popBox").remove();
                        })
                    }else {
                        pop(result.message,function () {
                            $(".popBox").remove();
                        })

                    }
                }
            }
        )
    })

    // 充值
    $("#rechargeBTN").click(function () {
        par.number = $("#recharge input[name=number]").val()
        par.bankCardNumber = $("#recharge input[name=bankCardNumber]").val()
        par.paymentPassword = $("#recharge input[name=paymentPassword]").val()
        var data = JSON.stringify(par);
        $.ajax({
                url: "/p2p/recharge",
                contentType: "application/json",
                async: false,
                datatype: "json",
                data: data,
                type: 'POST',
                success: function (result) {
                    if(result.statusCode=="5001"){
                        cookieNull(result.statusCode);
                    }
                    if(result.statusCode=="2000"){
                        pop(result.message,function(){
                            $("#notAdmin").click();
                            $("input").val("");
                            $(".popBox").remove();
                        })
                    }else {
                        pop(result.message,function () {
                            $(".popBox").remove();
                        })

                    }
                }
            }
        )
    });

    // 投资信息
    $("#queryinvestmentFont").click(function () {
        $('#queryinvestment').empty();
        var data = JSON.stringify(par);
        $.ajax({
                url: "/p2p/queryinvestment",
                contentType: "application/json",
                async: false,
                datatype: "json",
                data: data,
                type: 'POST',
                success: function (result) {
                    if (result.statusCode == '5001') {
                        cookieNull(result.statusCode);
                    }
                    var data = result.data
                    //console.log(data.length)
                    if (data.length == 0){
                        pop("暂无投资项目",function () {
                            $(".popBox").remove();
                            window.location.href='/p2p/static/index.html';
                        });
                        return
                    }
                    for (var i = 0; i < data.length; i++) {
                        $('#queryinvestment').append(
                            '<a class="row-item g-cf "target="_blank" id="message-btn" data-name="' + data[i].investmentId + '">' +
                            '<div class="label-content"></div>' +
                            '<dl class="col-1">' +
                            '<dt>' +
                            '<span class="text-tag" id="projectId" style="font-size: 20px; color:#ef7a46 ;">' + data[i].investmentProject + '</span>' +
                            '</dt>' +
                            '</dl>' +
                            '<dl class="col-2">' +
                            '<dt><span class="bold">' + data[i].investmentTime + '</span></dt>' +
                            '<dd>投资时间</dd>' +
                            '</dl>' +
                            '<dl class="col-3">' +
                            '<dt>' + data[i].investmentAmount + '</dt>' +
                            '<dd>投资金额</dd>' +
                            '</dl>' +
                            '<dl class="col-5">' +
                            '<dd class="item">' +
                            '<em class="btn-primary enabled" >债权转让</em>' +
                            '</dd>' +
                            '</dl>' +
                            '</a');
                        if(data[i].biddingStatus==0){
                            $("#queryinvestment em").html("正在回款")
                        } if(data[i].biddingStatus==2){
                            $("#queryinvestment em").html("回款结束")
                        }

                    }

                    $("#queryinvestment").on('click', '#message-btn', function (event) {
                        var investmentId = $(this).data('name')
                        par.investmentId =investmentId
                        if(investmentId == ""){
                            return
                        }
                        var data = JSON.stringify(par)
                        $.ajax({
                            url: "/p2p/transferCalculation",
                            contentType: "application/json",
                            async: false,
                            datatype: "json",
                            data: data,
                            type: 'POST',
                            success: function (result) {
                                if (result.statusCode == '5001') {
                                    cookieNull(result.statusCode);
                                }else{
                                    pop(result.message,function(){
                                        $(".popBox").remove();
                                    })
                                }
                                // console.log(result)
                                var data = result.data
                                var principal = "投资金额:"+data.principal
                                console.log(principal)
                                var income = "到手金额:"+data.income
                                console.log(income)
                                var interest = "利息:"+data.interest
                                console.log(interest)
                                var day = "投资天数:"+data.day
                                console.log(day)
                                var message = principal+","+income+","+interest+","+day
                                console.log(message)
                                $(".message p").text(message+"。是否继续转出")
                                $(".popBox").remove();
                                pop(message+"。是否继续转出",function(){
                                    par.interest = data.interest;
                                    par.principal =data.principal;
                                    var pdata = JSON.stringify(par);
                                    console.log(pdata)
                                    $.ajax({
                                        url: "/p2p/transfer",
                                        contentType: "application/json",
                                        async: false,
                                        datatype: "json",
                                        data: pdata,
                                        type: 'POST',
                                        success: function (result) {
                                            if (result.statusCode == '5001') {
                                                cookieNull(result.statusCode);
                                            }if(result.statusCode == "2000"){
                                                // console.log(result);
                                                $("input").val("");
                                                $(".popBox").remove();
                                                $('#queryinvestment').empty();
                                                $("#queryinvestmentFont").click();
                                            }else {
                                                pop(result.message,function () {
                                                    $(".popBox").remove();
                                                })

                                            }



                                        }
                                    })

                                })
                            }
                        })
                    })
                }
            }
        )
    })
    // 提现
    $("#consumptionBTN").click(function () {
        par.balance = $("#consumption input[name=balance]").val()
        par.bankCardNumber = $("#consumption input[name=bankCardNumber]").val()
        par.paymentPassword = $("#consumption input[name=paymentPassword]").val()
        var data = JSON.stringify(par);
        $.ajax({
                url: "/p2p/consumption",
                contentType: "application/json",
                async: false,
                datatype: "json",
                data: data,
                type: 'POST',
                success: function (result) {
                    if (result.statusCode == '5001') {
                        cookieNull(result.statusCode);
                    }
                    if(result.statusCode=="2000"){
                        pop(result.message,function(){
                            $("#notAdmin").click();
                            $("input").val("");
                            $(".popBox").remove();
                        })
                    }else {
                        pop(result.message,function () {
                            $(".popBox").remove();
                        })

                    }
                }
            }
        )
    })
    // 解绑卡
    $("#ubcardBTN").click(function () {
        par.bankCardNumber = $("#ubcard input[name=bankCardNumber]").val()
        par.paymentPassword = $("#ubcard input[name=paymentPassword]").val()
        var data = JSON.stringify(par);
        $.ajax({
                url: "/p2p/ubcard",
                contentType: "application/json",
                async: false,
                datatype: "json",
                data: data,
                type: 'POST',
                success: function (result) {
                    if (result.statusCode == '5001') {
                        cookieNull(result.statusCode);
                    }
                    if(result.statusCode=="2000"){
                        pop(result.message,function(){
                            $("#notAdmin").click();
                            $("input").val("");
                            $(".popBox").remove();
                        })
                    }else {
                        pop(result.message,function () {
                            $(".popBox").remove();
                        })

                    }
                }
            }
        )
    })
    // 修改支付密码
    $("#chpwdBTN").click(function () {
        par.user=$.cookie('phone');
        par.originalPasswd = $("#chpwd input[name=originalPasswd]").val()
        par.newPasswd = $("#chpwd input[name=newPasswd]").val()
        par.repeatPasswd = $("#chpwd input[name=repeatPasswd]").val()
        var data = JSON.stringify(par);
        $.ajax({
                url: "/p2p/chpwd",
                contentType: "application/json",
                async: false,
                datatype: "json",
                data: data,
                type: 'POST',
                success: function (result) {
                    if (result.statusCode == '5001') {
                        cookieNull(result.statusCode);
                    }
                    if(result.statusCode=="2000"){
                        pop(result.message,function(){
                            $("#notAdmin").click();
                            $("input").val("");
                            $(".popBox").remove();
                        })
                    }else {
                        pop(result.message,function () {
                            $(".popBox").remove();
                        })

                    }
                }
            }
        )
    })

    // 修改登录密码
    $("#chpwdLoginBTN").click(function () {
        par.user=$.cookie('phone');
        par.originalPasswd = $("#chpwdLogin input[name=originalPasswd]").val()
        par.newPasswd = $("#chpwdLogin input[name=newPasswd]").val()
        par.repeatPasswd = $("#chpwdLogin input[name=repeatPasswd]").val()
        var data = JSON.stringify(par);
        $.ajax({
                url: "/p2p/chpwd",
                contentType: "application/json",
                async: false,
                datatype: "json",
                data: data,
                type: 'POST',
                success: function (result) {
                    if (result.statusCode == '5001') {
                        cookieNull(result.statusCode);
                    }
                    if(result.statusCode=="2000"){
                        pop(result.message,function(){
                            $("#notAdmin").click();
                            $("input").val("");
                            $(".popBox").remove();
                            $.cookie("token", null);
                            $.cookie("phone", null);
                            window.location.href = '/p2p/static/index.html';
                        })
                    }else {
                        pop(result.message,function () {
                            $(".popBox").remove();
                        })

                    }
                }
            }
        )
    })
    // 注销账号（删除账号信息）
    $("#LogoutBTN").click(function () {
        par.passWd = $("#Logout input[name=passWd]").val()
        var data = JSON.stringify(par);
        $.ajax({
                url: "/p2p/Logout",
                contentType: "application/json",
                async: false,
                datatype: "json",
                data: data,
                type: 'POST',
                success: function (result) {
                    if (result.statusCode == '5001') {
                        cookieNull(result.statusCode);
                    } if (result.statusCode == '2000') {
                        pop(result.message,function () {
                            $("input").val("");
                            $(".popBox").remove();
                            $.cookie("token", null);
                            $.cookie("phone", null);
                            window.location.href = '/p2p/static/index.html';
                        });
                    } else {
                        pop(result.message,function () {
                            $(".popBox").remove();
                        })

                    }
                }
            }
        )
    })

    // 发布项目
    $("#proReleaseBTN").click(function () {
        console.log($.cookie('phone'));
        console.log($.cookie('token'));
        par.projectName = $("#proRelease input[name=projectName]").val()
        par.totalProject = $("#proRelease input[name=totalProject]").val()
        par.projectDeadline = $("#proRelease input[name=projectDeadline]").val()
        par.annualInterestRate = $("#proRelease input[name=annualInterestRate]").val()
        par.extraInterestRate = $("#proRelease input[name=extraInterestRate]").val()
        par.releaseTime = time($("#proRelease input[name=releaseTime]").val())
        var data = JSON.stringify(par);
        $.ajax({
                url: "/p2p/proRelease",
                contentType: "application/json",
                async: false,
                datatype: "json",
                data: data,
                type: 'POST',
                success: function (result) {
                    if (result.statusCode == '5001') {
                        cookieNull(result.statusCode);
                    } if (result.statusCode == '2000') {
                        pop(result.message,function () {
                            $("input").val("");
                            $(".popBox").remove();
                        });
                    }else {
                        pop(result.message,function () {
                            $(".popBox").remove();
                        })

                    }
                }
            }
        )
    })
});

//跑批
$(function(){
    $("#runBatch").click(function(){
        $.ajax({
            url:"/p2p/Payback",
            success: function (result) {
                if (result.statusCode == '5001') {
                    cookieNull(result.statusCode);
                } else {
                    pop(result.message,function () {
                        $(".popBox").remove();
                    });
                }
            }
        })
    })
})
